import config from '@/common/config';
import * as axios from 'axios';
import { AxiosResponse, AxiosRequestConfig } from 'axios';

const api = axios.default.create({
    baseURL: config.baseUrl,
    timeout: 30000,
});

api.interceptors.request.use(
    (config: AxiosRequestConfig) => config,
    (error: any) => Promise.reject(error),
);

api.interceptors.response.use(
    (response: AxiosResponse) => {
        if (response.status !== 200) {
            return Promise.reject(response);
        } else {
            return response;
        }
    },
    (error: any) => Promise.reject(error),
);

function get(url: string, params?: object): any {
    return new Promise((resolve, reject) => {
        api.get(url, {params}).then((res) => {
            resolve(res);
        }).catch((e) => {
            reject(e);
        });
    });
}

export default get;
